home *** CD-ROM | disk | FTP | other *** search
/ Developer CD Series 2000 August: Tool Chest / Dev.CD Aug 00 TC Disk 2.toast / pc / sample code / human interface toolbox / setwindbackcolor / setwindbackcolor.c next >
Encoding:
C/C++ Source or Header  |  2000-06-23  |  3.4 KB  |  137 lines

  1. /*
  2.     File:        SetWindBackColor.c
  3.  
  4.     Contains:    Demonstrates how to programmatically set the background color of a window 
  5.                 without flicker.
  6.  
  7.     Written by:    Pete Gontier     
  8.  
  9.     Copyright:    Copyright © 1997-1999 by Apple Computer, Inc., All Rights Reserved.
  10.  
  11.                 You may incorporate this Apple sample source code into your program(s) without
  12.                 restriction. This Apple sample source code has been provided "AS IS" and the
  13.                 responsibility for its operation is yours. You are not permitted to redistribute
  14.                 this Apple sample source code as "Apple sample source code" after having made
  15.                 changes. If you're going to re-distribute the source, we require that you make
  16.                 it clear in the source that the code was descended from Apple sample source
  17.                 code, but that you've made changes.
  18.  
  19.     Change History (most recent first):
  20.                 8/9/1999    Karl Groethe    Updated for Metrowerks Codewarror Pro 2.1
  21.                 
  22.  
  23. */
  24. #include <Sound.h>
  25. #define OLDROUTINELOCATIONS        0
  26. #define OLDROUTINENAMES            0
  27. #define SystemSevenOrLater        1
  28.  
  29. #ifndef __FONTS__
  30. #    include <Fonts.h>
  31. #endif
  32.  
  33. #ifndef __DIALOGS__
  34. #    include <Dialogs.h>
  35. #endif
  36.  
  37. #ifndef __QDOFFSCREEN__
  38. #    include <QDOffscreen.h>
  39. #endif
  40.  
  41. static pascal OSErr InitMac (void)
  42. {
  43.     MaxApplZone ( );
  44.     InitGraf (&(qd.thePort));
  45.     InitFonts ( );
  46.     InitWindows ( );
  47.     InitMenus ( );
  48.     TEInit ( );
  49.     InitDialogs (nil);
  50.  
  51.     return noErr;
  52. }
  53.  
  54. static pascal Boolean SetColorTableEntry (CTabHandle cth, short value, const RGBColor *rgbP)
  55. {
  56.     ColorSpecPtr    ctTable        = (**cth).ctTable;
  57.     short            ctSize        = (**cth).ctSize;
  58.  
  59.     while (ctSize > -1)
  60.     {
  61.         if (ctTable->value == value)
  62.         {
  63.             ctTable->rgb = *rgbP;
  64.             CTabChanged (cth);
  65.             return true;
  66.         }
  67.  
  68.         ++ctTable;
  69.         --ctSize;
  70.     }
  71.  
  72.     return false;
  73. }
  74.  
  75. void main (void)
  76. {
  77.     if (InitMac ( ))
  78.         SysBeep (10);
  79.     else
  80.     {
  81.         WindowRef        window;
  82.         Rect            boundsRect        = qd.screenBits.bounds;
  83.  
  84.         //
  85.         //    [1] Create a window which covers most of the main screen.
  86.         //    Make it invisible for now.
  87.         //
  88.         //    [2] Get the window's color table.
  89.         //
  90.         //    [3] 'winCTabHandle' is the default color table because we've
  91.         //    just now created the window out of thin air. We don't want
  92.         //    to change the default color table, so make a copy.
  93.         //
  94.         //    [4] Set the background entry (0) of the color table.
  95.         //
  96.         //    [5] Set the window's color table.
  97.         //
  98.         //    [6] Up till now, none of the changes we've made have been
  99.         //    visible. That's the way we've wanted it because the whole
  100.         //    purpose here is to avoid visible changes, which result in
  101.         //    flicker.
  102.         //
  103.         //    [7] Wait for the user to click,
  104.         //    then put a smart-aleck message in the debugger.
  105.         //
  106.  
  107.         InsetRect (&boundsRect,10,10);                                                        // 1
  108.         boundsRect.top += GetMBarHeight ( );                                                // 1
  109.         window = NewCWindow (nil,&boundsRect,"\p",false,plainDBox,(WindowRef)-1,false,0);    // 1
  110.         if (window)                                                                            // 1
  111.         {
  112.             WCTabHandle        winCTabHandle;                                                    // 2
  113.             AuxWinHandle    auxWinHandle;                                                    // 2
  114.  
  115.             GetAuxWin (window,&auxWinHandle);                                                // 2
  116.             winCTabHandle = (WCTabHandle) ((**auxWinHandle).awCTable);                        // 2
  117.  
  118.             HandToHand ((Handle *) &winCTabHandle);                                            // 3
  119.             if (!MemError ( ))                                                                // 3
  120.             {
  121.                 RGBColor blackness = { 0, 0, 0 };                                            // 4
  122.  
  123.                 if (SetColorTableEntry ((CTabHandle) winCTabHandle, 0, &blackness))            // 4
  124.                 {
  125.                     SetWinColor (window,winCTabHandle);                                        // 5
  126.  
  127.                     ShowWindow (window);                                                    // 6
  128.  
  129.                     while (!Button ( )) ;                                                    // 7
  130.                     DebugStr ("\pNotice any flicker? I thought not.");                        // 7
  131.                 }
  132.             }
  133.             DisposeWindow (window);
  134.         }
  135.     }
  136. }
  137.